package com.esm.sm800.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.esm.sm800.R;
import com.esm.sm800.database.dbmodel.Member;
import com.esm.sm800.database.manager.MemberCenter;
import com.esm.sm800.database.manager.SystemCenter;
import com.esm.sm800.util.MyFunc;
import com.esm.sm800.util.NFCComm;
import com.esm.sm800.util.Utils;
import com.esm.sm800.util.ZazManager;

import java.text.SimpleDateFormat;
import java.util.Date;

import android_serialport_api.ComBean;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.util.EditTextUtil;

//新增编辑人员
public class RyxzActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {

    private final String TAG = "RyxzActivity";

    private EditText et_name;
    private EditText et_zhuhao;
    TextView tv_jiaose;

    EditText et_acount;
    EditText et_pwd;

    TextView tv_zhiwen;
    TextView tv_nfc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_ryxz);

        et_name = (EditText) findViewById(R.id.et_name);
        et_zhuhao = (EditText) findViewById(R.id.et_zhuhao);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        et_acount = (EditText) findViewById(R.id.et_acount);
        tv_jiaose = (TextView) findViewById(R.id.tv_jiaose);
        tv_zhiwen = (TextView) findViewById(R.id.tv_zhiwen);
        tv_nfc = (TextView) findViewById(R.id.tv_nfc);

        findViewById(R.id.tv_del).setOnClickListener(this);
        findViewById(R.id.tv_save).setOnClickListener(this);
        findViewById(R.id.tv_zhiwenluru).setOnClickListener(this);
        findViewById(R.id.tv_back).setOnClickListener(this);
        findViewById(R.id.tv_jiaose).setOnClickListener(this);

        findViewById(R.id.et_name).setOnClickListener(this);
        findViewById(R.id.et_zhuhao).setOnClickListener(this);
        findViewById(R.id.et_pwd).setOnClickListener(this);
        findViewById(R.id.et_acount).setOnClickListener(this);

        if (SystemCenter.getInstance().memberEdit != null) {
            _isadd = false;
            et_name.setText(SystemCenter.getInstance().memberEdit.getName());
            et_zhuhao.setText(SystemCenter.getInstance().memberEdit.getRemark());
            et_pwd.setText(SystemCenter.getInstance().memberEdit.getPassword());
            et_acount.setText(SystemCenter.getInstance().memberEdit.getAccount());
            tv_zhiwen.setText(SystemCenter.getInstance().memberEdit.getZhiwen());
            tv_nfc.setText(SystemCenter.getInstance().memberEdit.getNfc());
            _memberType = SystemCenter.getInstance().memberEdit.getType();
        } else {
            findViewById(R.id.tv_del).setVisibility(View.GONE);
        }

        Member memberEdit = SystemCenter.getInstance().memberEdit;

        String[] TYPES = getResources().getStringArray(R.array.member_types);
        if (_memberType >= 0 && _memberType < TYPES.length) {
            tv_jiaose.setText(TYPES[_memberType]);
        }

        NFCComm.getInstance().mHandler = mHandler;
    }

    boolean _isadd = true;//false-编辑

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static final int REFRESH_UI = 1101;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH_UI) {

            } else if (msg.what == NFCComm.RECEIVED_MSG) {
                showMsg(NFCComm.getInstance().mComRecData);
            }
        }
    };

    private void showMsg(ComBean comRecData) {
        if (comRecData == null) {
            return;
        }
        String strHex = MyFunc.ByteArrToHex(comRecData.bRec);
        tv_nfc.setText(strHex);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_save:
                onSave();
                break;
            case R.id.tv_zhiwenluru:
                onZwlr();
                break;
            case R.id.tv_del:
                onDel();
                break;
            case R.id.tv_jiaose:
                onJiaose();
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.et_name:
                EditTextUtil.showKeyboard(context, et_name, true);
                break;
            case R.id.et_zhuhao:
                EditTextUtil.showKeyboard(context, et_zhuhao, true);
                break;
            case R.id.et_pwd:
                EditTextUtil.showKeyboard(context, et_pwd, true);
                break;
            case R.id.et_acount:
                EditTextUtil.showKeyboard(context, et_acount, true);
                break;
        }
    }

    private void onDel() {
        new AlertDialog(context, "", getString(R.string.qqdsfsc), true, 11, this).show();
    }

    private void onJiaose() {
        toActivity(BottomMenuWindow.createIntent(getBaseContext(), getResources().getStringArray(R.array.member_types))
                .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.member_jiaose)), REQUEST_TO_BOTTOM_MENU_1, false);
    }

    private void onZwlr() {
        if (Utils.isFastDoubleClick()) {
            return;
        }

        if (!doSave()) {
            return;
        }

        if (SystemCenter.getInstance().memberEdit.getZhiwen().length() > 0) {
            new AlertDialog(context, "", getString(R.string.sfcxlrzw), true, 116, this).show();
            return;
        }

        //需有member的id后才能存指纹
        Intent intent = new Intent(this, ZwlrActivity.class);
        startActivityForResult(intent, 103);
    }

    private void onSave() {
        if (doSave()) {
            setResult(RESULT_OK);
            finish();
        }

    }

    boolean doSave() {
        //获取当前日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date();
        String strDate = formatter.format(curDate);
        //

        String name = et_name.getText().toString().replace(" ", "").replace("\n", "");
        String zhuhao = et_zhuhao.getText().toString().replace(" ", "").replace("\n", "");
        String mima = et_pwd.getText().toString().replace(" ", "").replace("\n", "");
        String acount = et_acount.getText().toString().replace(" ", "").replace("\n", "");
        String nfc = tv_nfc.getText().toString();

        if (acount.length() < 1 || mima.length() < 1) {
            showShortToast(getString(R.string.qsrzhhmm));
            return false;
        }

        Member member = SystemCenter.getInstance().memberEdit;
        if (member == null) {
            member = new Member();
            member.setCreat_time(strDate);
            SystemCenter.getInstance().memberEdit = member;
        }
        member.setName(name);
        member.setRemark(zhuhao);
        member.setPassword(mima);
        member.setAccount(acount);
        member.setType(_memberType);
        member.setUpdate_time(strDate);
        member.setNfc(nfc);

        long id = MemberCenter.getInstance().saveMember(member);
        member.setId(id);

        return true;
    }


    @Override
    protected void onResume() {
        super.onResume();
        NFCComm.getInstance().mHandler = mHandler;
    }

    @Override
    protected void onPause() {
        super.onPause();
        NFCComm.getInstance().mHandler = null;
    }

    private final int REQUEST_TO_BOTTOM_MENU_1 = 31;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int pos = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    String[] TYPES = getResources().getStringArray(R.array.member_types);
                    if (pos >= 0 && pos < TYPES.length) {
                        String name = TYPES[pos];
                        _memberType = pos;
                        tv_jiaose.setText(name);
                    }
                }
                break;
            case 103:
                tv_zhiwen.setText(SystemCenter.getInstance().memberEdit.getZhiwen());
                break;
        }
    }

    int _memberType = 0;

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }
        if (requestCode == 11) {
            //确定删除
            ZazManager.getInstance().m_nUserID = Math.toIntExact(SystemCenter.getInstance().memberEdit.getId());
            ZazManager.getInstance().deleteID();//删除对应的指纹
            MemberCenter.getInstance().delMember(SystemCenter.getInstance().memberEdit);
            SystemCenter.getInstance().memberEdit = null;
            setResult(RESULT_OK);
            finish();
        } else if (requestCode == 116) {
            //先删除原指纹
            ZazManager.getInstance().m_nUserID = Math.toIntExact(SystemCenter.getInstance().memberEdit.getId());
            if (ZazManager.getInstance().deleteID()) {
                SystemCenter.getInstance().memberEdit.setZhiwen("");
                Intent intent = new Intent(this, ZwlrActivity.class);
                startActivityForResult(intent, 103);
            } else {
                showShortToast(getString(R.string.sczwcw));
            }

        }
    }
}
